home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / prog / basic / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  1.2 KB  |  78 lines

  1. #include <LEDA/list.h>
  2. #include <LEDA/stream.h>
  3.  
  4.  
  5.  
  6.  
  7. int cmp_inv(const string& x, const string& y)    { return compare(y,x); }
  8.  
  9. int cmp_length(const string& x, const string& y) 
  10. { return x.length() - y.length(); }
  11.  
  12. int ord(const string& x)   { return (x.length() > 0) ? int(x[0]) : 0;}
  13.  
  14. void upper_case(string& x)
  15. { int d = 'A' - 'a';
  16.   for(int i=0; i<x.length(); i++) 
  17.     if (x[i] >= 'a' && x[i] <= 'z') x[i] += d;
  18.  }
  19.  
  20.  
  21. main()
  22.   string fname = read_string("file: ");
  23.  
  24.   list<string> L;
  25.  
  26.   if (fname == "cin")
  27.      L.read("L = ");
  28.   else
  29.      L.read(file_istream(fname));
  30.  
  31.   L.permute();
  32.  
  33.  
  34. // sort lexicographically
  35.  
  36.   L.sort();   // compare(string,string) used
  37.   L.print("sorted lexicographically:\n",'\n');
  38.   newline;
  39.   newline;
  40.  
  41. // sort decreasing
  42.  
  43.   L.sort(cmp_inv);
  44.   L.print("sorted decrasing:\n",'\n');
  45.   newline;
  46.   newline;
  47.  
  48.  
  49. // sort by length
  50.  
  51.   L.sort(cmp_length);
  52.   L.print("sorted by length:\n",'\n');
  53.   newline;
  54.   newline;
  55.  
  56.  
  57.  
  58. // bucket_sort by first character
  59.  
  60.   L.bucket_sort(0,255,ord);
  61.   L.print("sorted by s[0]:\n",'\n');
  62.   newline;
  63.   newline;
  64.  
  65.  
  66. // turn characters to upper case
  67.  
  68.   L.apply(upper_case);
  69.   L.print("upper case:\n",'\n');
  70.   newline;
  71.  
  72.   newline;
  73.   cout << "used memory = " << used_memory() << " bytes\n";
  74.  
  75.   return 0;
  76. }
  77.